﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
using System.Windows.Forms.DataVisualization.Charting;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitData();
            Thread.CurrentThread.CurrentCulture = new CultureInfo("th-TH", false);
         
          
        }

        public double[] getData()
        {

            //fetches the data from datagridview
            int size = dataGridView1.Rows.Count;
            double[] result = new double[size-1];
            for (int i = 0; i < size-1; i++)
            {
                int idx;
               // if(Int32.TryParse(dataGridView1[0, i].Value.ToString(),out idx))
                result[i] = Convert.ToDouble(dataGridView1[1, i].Value);
            }
            return result;
        }



        public void InitData()
        {

            //initializes datagridview
            dataGridView1.Rows.Add(new String[]{"1","528.2",""});
            dataGridView1.Rows.Add(new String[] { "2", "542.8", "" });
            dataGridView1.Rows.Add(new String[] { "3", "531.2", "" });
            dataGridView1.Rows.Add(new String[] { "4", "597.5", "" });
            dataGridView1.Rows.Add(new String[] { "5", "555.9", "" });
            dataGridView1.Rows.Add(new String[] { "6", "553.2", "" });
            dataGridView1.Rows.Add(new String[] { "7", "528.2", "" });
            dataGridView1.Rows.Add(new String[] { "8", "539.2", "" });
            dataGridView1.Rows.Add(new String[] { "9", "548.2", "" });
            dataGridView1.Rows.Add(new String[] { "10", "523.5", "" });
            dataGridView1.Rows.Add(new String[] { "11", "546.8", "" });
            dataGridView1.Rows.Add(new String[] { "12", "545.8", "" });
            dataGridView1.Rows.Add(new String[] { "13", "538.4", "" });
            dataGridView1.Rows.Add(new String[] { "14", "541.7", "" });
            dataGridView1.Rows.Add(new String[] { "15", "547.6", "" });
            dataGridView1.Rows.Add(new String[] { "16", "541.9", "" });
            dataGridView1.Rows.Add(new String[] { "17", "551.0", "" });
            dataGridView1.Rows.Add(new String[] { "18", "540.0", "" });
            dataGridView1.Rows.Add(new String[] { "19", "569.8", "" });
            dataGridView1.Rows.Add(new String[] { "20", "529.3", "" });
            dataGridView1.Rows.Add(new String[] { "21", "524.2", "" });
            dataGridView1.Rows.Add(new String[] { "22", "558.5", "" });
            dataGridView1.Rows.Add(new String[] { "23", "544.0", "" });
            dataGridView1.Rows.Add(new String[] { "24", "545.4", "" });
            dataGridView1.Rows.Add(new String[] { "25", "539.6", "" });
            dataGridView1.Rows.Add(new String[] { "26", "525.5", "" });
            dataGridView1.Rows.Add(new String[] { "27", "592.2", "" });
            dataGridView1.Rows.Add(new String[] { "28", "536.8", "" });
            dataGridView1.Rows.Add(new String[] { "29", "519.9", "" });
            dataGridView1.Rows.Add(new String[] { "30", "505.1", "" });
            dataGridView1.Rows.Add(new String[] { "31", "536.0", "" });
            dataGridView1.Rows.Add(new String[] { "32", "584.5", "" });
            dataGridView1.Rows.Add(new String[] { "33", "540.3", "" });
            dataGridView1.Rows.Add(new String[] { "34", "544.5", "" });
            dataGridView1.Rows.Add(new String[] { "35", "535.0", "" });
            dataGridView1.Rows.Add(new String[] { "36", "551.3", "" });
            dataGridView1.Rows.Add(new String[] { "37", "558.3", "" });
            dataGridView1.Rows.Add(new String[] { "38", "525.5", "" });
            dataGridView1.Rows.Add(new String[] { "39", "554.7", "" });
            dataGridView1.Rows.Add(new String[] { "40", "542.1", "" });
         
        }

     
        private void button1_Click(object sender, EventArgs e)
        {
            double[] Data = getData();
            bool []Error = new bool[Data.Length];
            double []U=new double[Data.Length];
            if(radioButton1.Checked)
            Statistics.getErrorsMethod1(Data, Error,U,0.05);
            else
            Statistics.getErrorsMethod2(Data, Error, U, 0.05);
            double[] Ar = getData();
            chart1.Series[0].Points.Clear();
            chart1.Series[1].Points.Clear();
            chart1.ChartAreas["Default"].Area3DStyle.Enable3D = true;

            // Set Rotation angles
            chart1.ChartAreas["Default"].Area3DStyle.Rotation = 10;
            chart1.ChartAreas["Default"].Area3DStyle.Inclination = 10;
            chart1.Series[1].XAxisType=AxisType.Secondary;
            for (int i = 0; i < Ar.Length; i++)
            {
                dataGridView1[2, i].Value = U[i];
                if (!Error[i])
                {
                    chart1.Series[0].Points.Add(new DataPoint(i + 1, Ar[i]));
                    dataGridView1[3, i].Value = "NO";
                }
                else
                {
                    chart1.Series[0].Points.Add(new DataPoint(i + 1, Ar[i]));
                    chart1.Series[0].Points[i].Color = Color.Red;
                    dataGridView1[3, i].Value = "YES";
                }
               
            }
            
        }
       

        private void chart1_Click_1(object sender, EventArgs e)
        {

        }

        private void Distribution_Histogramm_Click(object sender, EventArgs e)
        {

        }
    }
}
